package com.lightboat.web.controller.system;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lightboat.common.annotation.Log;
import com.lightboat.common.core.controller.BaseController;
import com.lightboat.common.core.domain.AjaxResult;
import com.lightboat.common.core.page.TableDataInfo;
import com.lightboat.common.enums.BusinessType;
import com.lightboat.common.utils.PageUtils;
import com.lightboat.common.utils.SecurityUtils;
import com.lightboat.system.domain.SysNotice;
import com.lightboat.system.service.ISysNoticeService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 公告 信息操作处理
 *
 * @author lightboat
 */
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController {
    @Autowired
    private ISysNoticeService noticeService;

    /**
     * 获取通知公告列表
     */
    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysNotice notice) {
        startPage();
        PageUtils.<SysNotice>chainOrderBy().orderByAsc(SysNotice::getIsRead).orderByDesc(SysNotice::getCreateTime).apply();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        clearPage();
        return getDataTable(list);
    }

    /**
     * 获取用户通知公告列表
     *
     * @param notice
     * @return
     */
    @Operation(summary = "获取用户通知公告列表")
    @GetMapping("/user/list")
    public TableDataInfo listUser(SysNotice notice) {
        startPage();
        notice.setUserId(SecurityUtils.getUserId());
        notice.setStatus("0");
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        clearPage();
        return getDataTable(list);
    }

    /**
     * 用户读通知
     */
    @Operation(summary = "用户读通知")
    @PostMapping("/user/read")
    public AjaxResult readUser(@RequestParam Long noticeId) {
        boolean update = noticeService.update(
                Wrappers.<SysNotice>lambdaUpdate()
                        .set(SysNotice::getIsRead, 1)
                        .eq(SysNotice::getNoticeId, noticeId)
                        .eq(SysNotice::getUserId, SecurityUtils.getUserId())
        );
        return toAjax(update);
    }

    /**
     * 用户全读通知
     */
    @Operation(summary = "用户全读通知")
    @PostMapping("/user/read/all")
    public AjaxResult readAllUser() {
        boolean update = noticeService.update(
                Wrappers.<SysNotice>lambdaUpdate()
                        .set(SysNotice::getIsRead, 1)
                        .eq(SysNotice::getIsRead, 0)
                        .eq(SysNotice::getUserId, SecurityUtils.getUserId())
        );
        return toAjax(update);
    }

    /**
     * 用户消息统计
     */
    @Operation(summary = "用户消息统计")
    @GetMapping("/user/count")
    public AjaxResult countUser(String type) {
        Map<String, Object> objectMap = noticeService.countUser(type);
        return success(objectMap);
    }

    /**
     * 用户未读消息类型统计
     */
    @Operation(summary = "用户未读消息类型统计")
    @GetMapping("/user/unread/type/count")
    public AjaxResult countTypeUser(String type) {
        Map<String, Object> objectMap = noticeService.countUnReadMsgType(type);
        return success(objectMap);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable Long noticeId) {
        return success(noticeService.selectNoticeById(noticeId));
    }

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNotice notice) {
        notice.setCreateBy(getUsername());
        notice.setNoticeType("2");
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 修改通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysNotice notice) {
        notice.setUpdateBy(getUsername());
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds) {
        return toAjax(noticeService.deleteNoticeByIds(noticeIds));
    }
}
